package com.team.auth.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.team.auth.form.LoginByPhoneBody;
import com.team.auth.form.MerchantRegisterBody;
import com.team.auth.service.SysPasswordService;
import com.team.auth.service.SysRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.team.auth.form.LoginBody;
import com.team.auth.form.RegisterBody;
import com.team.auth.service.SysLoginService;
import com.team.common.core.domain.R;
import com.team.common.core.utils.JwtUtils;
import com.team.common.core.utils.StringUtils;
import com.team.common.security.auth.AuthUtil;
import com.team.common.security.service.TokenService;
import com.team.common.security.utils.SecurityUtils;
import com.team.system.api.model.LoginUser;

/**
 * token 控制
 * 
 * @author ruoyi
 */
@RestController
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @Resource
    private SysPasswordService sysPasswordService;

    @Resource
    private SysRegisterService sysRegisterService;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    @PostMapping("loginByPhone")
    public R<?> loginByPhone(@RequestBody LoginByPhoneBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.loginByPhone(form.getPhone(), form.getCode());
        // 判断用户是否存在
        if (StringUtils.isNull(userInfo))
        {
            return R.fail("用户不存在或手机号未注册");
        }
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    /*
    *  商家登录
    * */
    @PostMapping("loginByMerchant")
    public R<?> loginByMerchant(@RequestBody LoginByPhoneBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.loginByMerchant(form.getPhone(), form.getCode());
        // 判断用户是否存在
        if (StringUtils.isNull(userInfo))
        {
            return R.fail("用户不存在或手机号未注册");
        }
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    // 商家注册
    @PostMapping("registerByMerchant")
    public R<?> registerByMerchant(@RequestBody MerchantRegisterBody registerBody) {

        // 校验商家注册信息
        if (StringUtils.isEmpty(registerBody.getOwnerName()) ||
            StringUtils.isEmpty(registerBody.getIdNumber()) ||
            StringUtils.isEmpty(registerBody.getPhone()) ||
            StringUtils.isEmpty(registerBody.getVerifyCode()) ||
            StringUtils.isEmpty(registerBody.getShopName()) ||
            StringUtils.isEmpty(registerBody.getBusinessLicense()) ||
            StringUtils.isEmpty(registerBody.getArea()) ||
            StringUtils.isEmpty(registerBody.getAddress()) ||
            StringUtils.isEmpty(registerBody.getDescription()) ||
            registerBody.getLatitude() == null ||
            registerBody.getLongitude() == null) {
            return R.fail("商家注册信息不完整");
        }

        // 校验手机号格式
        if (!StringUtils.isPhoneNumber(registerBody.getPhone())) {
            return R.fail("手机号格式不正确");
        }
        // 校验身份证号码格式
        if (!StringUtils.isIdCard(registerBody.getIdNumber())) {
            return R.fail("身份证号码格式不正确");
        }
        // 校验验证码六位
        if (registerBody.getVerifyCode().length() == 6) {
            // 验证验证码是否正确
            if (!sysPasswordService.matchesPhoneAndCode(registerBody.getPhone(), registerBody.getVerifyCode())) {
                return R.fail("验证码错误或已过期");
            }
        }
        // 商家注册
        sysRegisterService.registerByMerchant(registerBody);
        return R.ok();
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }
}
